package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.healthcloud.dao.PastHistoryMapper;
import com.healthcloud.entity.PastHistory;
import com.healthcloud.service.PastHistoryService;

/**
 * Created by XYC on 2015/7/16.
 */
@Service
public class PastHistoryServiceImpl implements PastHistoryService {

    @Autowired
    private PastHistoryMapper pastHistoryMapper;

    @Override
    public ArrayList<PastHistory> findByIdCard(PastHistory pastHistory) {
        PastHistory pastHistory1= new PastHistory();
        pastHistory1.setIdCard(pastHistory.getIdCard());
        return findByEntity(pastHistory1);
    }

    @Override
    public ArrayList<PastHistory> findByEntity(PastHistory pastHistory) {
        return pastHistoryMapper.findByEnitiesWithOutPage(pastHistory);
    }

    @Override
    public Boolean saveList(ArrayList<PastHistory> pastHistories) {
        if(pastHistories.isEmpty()) {
            return false;
        }else{
            deletByIdCard(pastHistories.get(0));
            listAddDateTime(pastHistories);
            pastHistoryMapper.saveByEntities(pastHistories);
        }
        return true;
    }

    @Override
    public Boolean saveOrUpdata(PastHistory pastHistory,String pastMethod , Boolean operate) {
        changeMethodNameToCode(pastHistory,pastMethod);
        if(pastHistory.getGotDateOfString()!=null){
            pastHistory.setGotDate(pastHistory.StringTransformDateTime(pastHistory.getGotDateOfString()+" 00:00:00"));
        }
        if (operate){
            pastHistory.setUpdatedTime(new Date());
            pastHistory.setCreatedTime(new Date());
            pastHistoryMapper.save(pastHistory);
        }else{
            PastHistory pastHistory1 = pastHistoryMapper.get(pastHistory.getId());
            pastHistory.setUpdatedTime(new Date());
            pastHistory.setCreatedTime(pastHistory1.getCreatedTime());
            pastHistoryMapper.update(pastHistory);
        }
        return true;
    }

    @Override
    public Boolean deleByEntity(PastHistory pastHistory) {
        pastHistoryMapper.deletByEntity(pastHistory);
        return true;
    }

    @Override
    public Boolean deletByIdCard(PastHistory pastHistory) {
        PastHistory pastHistory1 =new PastHistory();
        pastHistory1.setIdCard(pastHistory.getIdCard());
        Boolean bool = deleByEntity(pastHistory1);
        return bool;
    }

    @Override
    public Boolean deleById(String idData) {
        String[] idDatas = idData.split("-");
        for (int i = 0; i < idDatas.length ; i++) {
            pastHistoryMapper.delete(Long.parseLong(idDatas[i]));
        }
        return true;
    }

    @Override
    public ArrayList<PastHistory> findByIdCardWithMethod(PastHistory pastHistory, String pastMethod) {
        PastHistory pastHistory1 = new PastHistory();
        pastHistory1.setIdCard(pastHistory.getIdCard());
        changeMethodNameToCode(pastHistory1,pastMethod);
        return findByEntity(pastHistory1);
    }

    //    @Override
    private void listAddDateTime(ArrayList<PastHistory> pastHistories) {
        for (int i = 0; i < pastHistories.size(); i++) {
            if (pastHistories.get(i).getGotDateOfString()!=null){
                pastHistories.get(i).setGotDate(pastHistories.get(i).StringTransformDateTime(pastHistories.get(i).getGotDateOfString().substring(0,10) + " 00:00:00"));
            }
            pastHistories.get(i).setCreatedTime(new Date());
            pastHistories.get(i).setUpdatedTime(new Date());
        }
    }

    @Override
    public void listDataTimeShow(ArrayList<PastHistory> pastHistories){
        for (int i = 0; i < pastHistories.size(); i++) {
            if (pastHistories.get(i).getGotDate()!=null){
                pastHistories.get(i).setGotDateOfString(pastHistories.get(i).DateTimeTransformString("yyyy-MM-dd", pastHistories.get(i).getGotDate()));
            }
        }
    }
    public void fillInAttribute( HashMap<String,HashMap> map,ArrayList<PastHistory> pastHistories){
        HashMap<Integer, String> diseaseHashMap = (HashMap<Integer, String>) map.get("disease");
        for (int i = 0; i <pastHistories.size() ; i++) {
            pastHistories.get(i).setDiseaseOfString(diseaseHashMap.get(pastHistories.get(i).getDisease()));
//            switch (pastHistories.get(i).getTypeCode()){
//                case "1":pastHistories.get(i).gets
//            }

        }
    }

    private void changeMethodNameToCode(PastHistory pastHistory, String pastMethod){
        switch (pastMethod){
            case "disease":pastHistory.setTypeCode("1");break;
            case "surgery":pastHistory.setTypeCode("2");break;
            case "Trauma":pastHistory.setTypeCode("3");break;
            case "transfusion":pastHistory.setTypeCode("4");break;
        }
    }
}

